package com.ywjh.inter.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.entity.PatientSportRecordEntity;
import com.ywjh.inter.entity.SportPlanEntity;
import com.ywjh.inter.entity.SportPlanItemEntity;
import com.ywjh.inter.service.SportService;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Sport extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8013544320349670091L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");
		JSONObject postData = StrUtil.getPostJSON(req);
		String act = postData.getString("act");
		JSONObject data = postData.getJSONObject("data");
		int r = 1;
		String e = "";
		JSONObject json = new JSONObject();
		switch (act) {
		case "add":
			int plan_id = StrUtil.getInt(data.getString("plan_id"), 0);
			int time = StrUtil.getInt(data.getString("time"), 0);
			int patient_id = StrUtil.getInt(data.getString("patient_id"), 0);
			String way = data.getString("way");
			PatientSportRecordEntity patientSportRecordEntity = new PatientSportRecordEntity();
			patientSportRecordEntity.setAdd_time(StrUtil.getTimestampHHMI(data.getString("add_time")));
			patientSportRecordEntity.setPatient_id(patient_id);
			patientSportRecordEntity.setTime(time);
			patientSportRecordEntity.setSport_plan_id(plan_id);
			patientSportRecordEntity.setWay(way);
			patientSportRecordEntity.setStatus(1);
			int id = SportService.getInstance().addPatientSportRecord(patientSportRecordEntity);
			if (id > -1) {
				r = 1;
				json.put("d", id);
			} else {
				r = 0;
				e = "数据库错误";
			}

			break;
		case "update":
			plan_id = StrUtil.getInt(data.getString("plan_id"), 0);
			time = StrUtil.getInt(data.getString("time"), 0);
			patient_id = StrUtil.getInt(data.getString("patient_id"), 0);
			way = data.getString("way");
			id = StrUtil.getInt(data.getString("id"), 0);
			patientSportRecordEntity = SportService.getInstance().findPatientSportRecordById(id);
			if (patientSportRecordEntity == null) {
				e = "未找到患者饮食计划";
			} else {
				patientSportRecordEntity.setAdd_time(StrUtil.getTimestampHHMI(data.getString("add_time")));
				patientSportRecordEntity.setPatient_id(patient_id);
				patientSportRecordEntity.setTime(time);
				patientSportRecordEntity.setSport_plan_id(plan_id);
				patientSportRecordEntity.setWay(way);
				patientSportRecordEntity.setStatus(1);
				boolean b = SportService.getInstance().updatePatientSportRecord(patientSportRecordEntity);
				if (b) {
					r = 1;
				} else {
					r = 0;
					e = "数据库错误";
				}
			}
			break;
		case "list_sport_records":
			int sport_plan_id = StrUtil.getInt(data.getString("sport_plan_id"), 0);
			patient_id = StrUtil.getInt(data.getString("patient_id"), 0);
			List<PatientSportRecordEntity> list = SportService.getInstance().listPatientRecordsByPlanId(sport_plan_id, patient_id);
			JSONArray array = new JSONArray();
			for (PatientSportRecordEntity entity : list) {
				JSONObject jsonObject = entity.toJson();
				array.add(jsonObject);
			}
			r = 1;
			json.put("d", array);
			break;
		case "delete_sport_record":
			id = StrUtil.getInt(data.getString("id"), 0);
			patientSportRecordEntity = SportService.getInstance().findPatientSportRecordById(id);
			patientSportRecordEntity.setStatus(0);
			boolean b = SportService.getInstance().updatePatientSportRecord(patientSportRecordEntity);
			if (b) {
				r = 1;
			} else {
				r = 0;
				e = "数据库错误";
			}

			break;
		case "list_sport_plan":
			patient_id = StrUtil.getInt(data.getString("patient_id"), 0);
			List<SportPlanEntity> planList = SportService.getInstance().listByPatientId(patient_id);
			array = new JSONArray();
			for (SportPlanEntity entity : planList) {
				JSONObject jsonObject = entity.toJson();
				List<SportPlanItemEntity> itemList=SportService.getInstance().listItemByPlanId(entity.getId());
				JSONArray itemArray=new JSONArray();
				for (SportPlanItemEntity itemEntity:itemList) {
					itemArray.add(itemEntity.toJson());
				}
				jsonObject.put("item_array", itemArray);
				array.add(jsonObject);
			}
			r = 1;
			json.put("d", array);
			break;

		}

		json.put("r", r);
		json.put("e", e);

		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();

	}
}
